﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static LiWi.Shell;

namespace LiWiTools
{
    public class DataControl
    {
        /// <summary>
        /// 控制卡数据更新标志
        /// </summary>
        public bool readFlag;
        /// <summary>
        /// 控制器名称
        /// </summary>
        public  string controlName;
        /// <summary>
        /// 控制器序列号
        /// </summary>
        public  uint controlID;
        /// <summary>
        /// 连接控制器轴数
        /// </summary>
        public int axisConnectNum;
        /// <summary>
        /// 控制器最大虚拟轴数
        /// </summary>
        public  ushort axisVirtuAxises;
        /// <summary>
        /// 控制器最大电机数量
        /// </summary>
        public  byte axisMaxMotor;
        /// <summary>
        /// 软件编号
        /// </summary>
        public  ushort softWare;
        /// <summary>
        /// 硬件编号
        /// </summary>
        public  ushort hardWare;
        /// <summary>
        /// 软件版本
        /// </summary>
        public  uint softVersion;
        /// <summary>
        /// 硬件版本
        /// </summary>
        public  uint hardVersion;
        /// <summary>
        /// 输入口数
        /// </summary>
        public  int INNum;
        /// <summary>
        /// 输出口数
        /// </summary>
        public  int OUTNum;
        /// <summary>
        /// 最大AD数
        /// </summary>
        public  int ADNum;
        /// <summary>
        /// 最大DA数
        /// </summary>
        public  int DANum;
        /// <summary>
        /// 当前IP
        /// </summary>
        public  string IP;
        /// <summary>
        /// 读取控制器数据
        /// </summary>
        public  string controlData;
        /// <summary>
        /// out口状态
        /// </summary>
        public  uint[] outStata;
        /// <summary>
        /// IN反转状态
        /// </summary>
        public  int[] inRev;


        /// <summary>
        /// 获取控制器参数
        /// </summary>
        public void GetControlData(Data IPName)
        {
            readFlag=true;
            PMC_GetMaxAxis(IPName.handle, ref axisConnectNum);                                        //读取连接控制器轴数
            byte[] maxio=new byte[4];
            PMC_GetSysSpecification(IPName.handle, ref axisVirtuAxises, ref axisMaxMotor, maxio);     //读取控制器最大规格数maxio:IN,OUT,AD,DA
            INNum =maxio[0];
            OUTNum =maxio[1];
            ADNum =maxio[2];
            DANum =maxio[3];
            PMC_GetIdSoftware(IPName.handle, ref softWare);                                           //读取软件编号
            PMC_GetIdHardware(IPName.handle, ref hardWare);                                           //读取硬件编号
            PMC_GetSoftwareVersion(IPName.handle, ref softVersion);                                   //读取软件版本
            PMC_GetHardVersion(IPName.handle, ref hardVersion);                                       //读取硬件版本
            byte[] name = new byte[20] ;
            PMC_GetControllerName(IPName.handle, name);                                               //读取控制器名称
            controlName=Encoding.ASCII.GetString(name).TrimEnd('\0');
            PMC_GetIdentifier(IPName.handle, ref controlID);                                          //读取控制器序列号
            byte[] IPByte = new byte[20] ;
            PMC_GetIp(IPName.handle, IPByte);                                                         //读取控制器当前IP
            IP= Encoding.ASCII.GetString(IPByte).TrimEnd('\0');
            byte[] data = new byte[1024] ;
            PMC_Execute(IPName.handle, "print *max", data, 1024);
            controlData = Encoding.ASCII.GetString(data).TrimEnd('\0');


            //重置out口数据
            outStata = new uint[OUTNum];
            inRev = new int[INNum];
            for (int i = 0; i < OUTNum; i++)
            {
                outStata[i] = 0;
            }
            for (int i = 0; i < INNum; i++)
            {
                inRev[i] = 0;
            }
        }
    }
}
